Public Job As String
Public Complete As Boolean
Public Ready As String
Public R As CodeMaxCtl.Range
Dim X As Long, Y As Long
Dim E As IHTMLEventObj

Public Sub ChangeMode(Mode As ModeConstant)

    Select Case Mode
        Case 1
            DHTML1.ZOrder
            DHTML1.SetFocus
        Case 2
            On Error Resume Next
            Code1.text = CreatorHTML
            Code1.ZOrder
            Code1.SetFocus
        Case 3
            CStatus(CInt(Me.Tag)).HTMLOpened = True
            ShowEditor
        Case 4
            If FStatus.OpenFromROM = False Then
                Load frmBrowser
                SaveText FStatus.TempFilename, CreatorHTML
                frmBrowser.Caption = Me.Caption
                frmBrowser.Show
                frmBrowser.brwWebBrowser.Navigate FStatus.TempFilename
            Else
                frmBrowser.Caption = Me.Caption
                frmBrowser.Show
                frmBrowser.brwWebBrowser.Navigate FStatus.OriginalFilename
            End If
    End Select
End Sub

Sub ShowEditor()
With frmCodeEditor
    If .CE1.IsExist(CInt(Me.Tag)) = False Then .AddEditor CInt(Me.Tag), FStatus.FileTitle, CreatorHTML
    .CE1.ShowEditorSet CInt(Me.Tag)
    .CE1.ActiveHTML = CreatorHTML
    .CE1.ActiveEditor.text = CreatorHTML
    .Show
End With
        'With editor(CInt(Me.Tag))
        '    .HTMLString = CreatorHTML
        '    .Code.text = CreatorHTML
        '    .Caption = FStatus.FileTitle
        '    .Show
        '    .Code.SetFocus
        'End With
End Sub

Private Sub Code1_SelChange(ByVal Control As CodeMaxCtl.ICodeMax)
Set R = Code1.GetSel(True)
Code1.HighlightedLine = R.EndLineNo
End Sub

Public Function GetSelText() As String
If LCase(DHTML1.DOM.selection.Type) = "text" Then
Dim rg As IHTMLTxtRange
Set rg = DHTML1.DOM.selection.createRange
GetSelText = rg.text
End If
End Function

Public Sub IncludeTag(TagName As String)
If LCase(DHTML1.DOM.selection.Type) = "text" Then
Dim rg As IHTMLTxtRange
Set rg = DHTML1.DOM.selection.createRange

rg.pasteHTML "<" & TagName & ">" & rg.text & "</" & TagName & ">"

End If
End Sub

Private Sub DHTML1_DisplayChanged()
'Work only when Ready="True"
If Ready = "True" Then
    
    RefreshMfrm
    DynaLink

End If

End Sub

Sub DynaLink()
On Error Resume Next
Dim a As String
Dim Ele As IHTMLElement

Set Ele = ElementP

If DHTML1.DOM.selection.Type = "None" Then
MfrmProgram.Sta.Panels(1).text = "{" & DHTML1.DOM.selection.Type & "} <" & Ele.TagName & ">"
Else
MfrmProgram.Sta.Panels(1).text = "{" & DHTML1.DOM.selection.Type & "} <" & GetActiveElement.TagName & ">"
End If

Select Case LCase(Ele.TagName)
    
    Case "a"
        MfrmProgram.Sta.Panels(2).text = Ele.href
    
    Case Else
        MfrmProgram.Sta.Panels(2).text = ""

End Select

End Sub

Sub EditBarCheck(TbrButton As MSComctlLib.Button, TheFunction As DHTMLEDITCMDID)
Dim n As DHTMLEDITCMDF
n = DHTML1.QueryStatus(TheFunction)
Select Case n
    Case 1      'Disabled
        TbrButton.Enabled = False
        TbrButton.Value = tbrUnpressed
    Case 3      'Enabled
        TbrButton.Enabled = True
        TbrButton.Value = tbrUnpressed
    Case 7      'Latched
        TbrButton.Enabled = True
        TbrButton.Value = tbrPressed
    Case 11    'Ninched
        TbrButton.Enabled = True
        TbrButton.Value = tbrUnpressed
    Case 0      'Not Supported
        TbrButton.Enabled = False
End Select
End Sub

Public Sub RefreshMfrm()

With MfrmProgram.tbrGeneral 'Refresh the General Bar first
    
    EditBarCheck .Buttons("cut"), DECMD_CUT
    EditBarCheck .Buttons("copy"), DECMD_COPY
    EditBarCheck .Buttons("paste"), DECMD_PASTE
    EditBarCheck .Buttons("delete"), DECMD_DELETE
    EditBarCheck .Buttons("undo"), DECMD_UNDO
    EditBarCheck .Buttons("redo"), DECMD_REDO
    
End With

With MfrmProgram.tbrEdit 'Refresh the Edit Bar next
    
    EditBarCheck .Buttons("bold"), DECMD_BOLD
    EditBarCheck .Buttons("italic"), DECMD_ITALIC
    EditBarCheck .Buttons("underline"), DECMD_UNDERLINE
    EditBarCheck .Buttons("left"), DECMD_JUSTIFYLEFT
    EditBarCheck .Buttons("center"), DECMD_JUSTIFYCENTER
    EditBarCheck .Buttons("right"), DECMD_JUSTIFYRIGHT
    EditBarCheck .Buttons("number"), DECMD_ORDERLIST
    EditBarCheck .Buttons("bullet"), DECMD_UNORDERLIST
    EditBarCheck .Buttons("outdent"), DECMD_OUTDENT
    EditBarCheck .Buttons("indent"), DECMD_INDENT
    
End With

With MfrmProgram 'Refresh other element finally
    On Error Resume Next
    If .Font1.text <> DHTML1.ExecCommand(DECMD_GETFONTNAME) Then .Font1.text = DHTML1.ExecCommand(DECMD_GETFONTNAME)
    If .cpBack.Color <> HTML2RGB(DHTML1.ExecCommand(DECMD_GETBACKCOLOR)) Then .cpBack.Color = HTML2RGB(DHTML1.ExecCommand(DECMD_GETBACKCOLOR))
    If .cpFore.Color <> HTML2RGB(DHTML1.ExecCommand(DECMD_GETFORECOLOR)) Then .cpFore.Color = HTML2RGB(DHTML1.ExecCommand(DECMD_GETFORECOLOR))
    If .cobFormat.text <> DHTML1.ExecCommand(DECMD_GETBLOCKFMT) Then .cobFormat.text = DHTML1.ExecCommand(DECMD_GETBLOCKFMT)
    If .cobSize.text <> DHTML1.ExecCommand(DECMD_GETFONTSIZE) Then .cobSize.text = DHTML1.ExecCommand(DECMD_GETFONTSIZE)
End With

End Sub

Private Sub DHTML1_DocumentComplete()
If Me.Job = "gethtml" Then
    'Get document HTML
    CStatus(CIndex).HTMLString = DHTML1.DocumentHTML
    Me.Job = ""
End If
End Sub

Private Sub DHTML1_onclick()
    On Error Resume Next
    If GetOption("showtoolbox", True) = True Then
        'Set frmToolBox.Element = GetActiveElement
        Set frmToolBox.TempEle = GetActiveElement
        frmToolBox.Code1.text = GetActiveElement.outerHTML
    End If
    
End Sub

Private Sub DHTML1_onmousemove()
On Error Resume Next
Set E = DHTML1.DOM.parentWindow.event
X = E.clientX
Y = E.clientY
End Sub

Private Sub DHTML1_ShowContextMenu(ByVal xPos As Long, ByVal yPos As Long)

PopupMenu MfrmProgram.mnuExt
End Sub

Public Sub Form_Activate()
'DoEvents
CheckAcc
On Error Resume Next
MfrmProgram.FileTab.Tabs("key" & CInt(Me.Tag)).Selected = True

    If GetOption("showtoolbox", True) = True Then
        Set frmToolBox.Element = Nothing
        frmToolBox.Code1.text = ""
    End If
    
End Sub

Public Sub UpdateTabCaption()

On Error GoTo NextStep
MfrmProgram.FileTab.Tabs(CInt(Me.Tag)).Caption = Me.Caption
NextStep:

End Sub

Public Sub CheckAcc()

Select Case FStatus.OpenFromROM

    Case True
        
        MfrmProgram.mnuSave = False
        tbrQuick.Buttons("editcode").Enabled = False
    
    Case False
        
        MfrmProgram.mnuSave = True
        tbrQuick.Buttons("editcode").Enabled = True
        
End Select

End Sub

Private Sub Form_Load()
Me.Show
DoEvents
    With DHTML1
       .ShowDetails = GetOption("ShowDetails", False)
        tbrQuick.Buttons(6).Value = IIf(.ShowDetails = True, 1, 0)
        .SnapToGrid = GetOption("SnapToGrid", True)
        tbrQuick.Buttons(7).Value = IIf(.SnapToGrid = True, 1, 0)
    End With

    With Code1
        .SetColor cmClrLeftMargin, &H808080
        .SetColor cmClrLineNumberBk, &H808080
        .SetColor cmClrLineNumber, vbWhite
        .HighlightedLine = 0
    End With

End Sub

Private Sub Form_Resize()

On Error Resume Next

DHTML1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight - 330

Code1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight - 330

End Sub

Private Function FStatus() As FormStatus
On Error GoTo 1
FStatus = CStatus(CInt(Me.Tag))
Exit Function
1
FStatus = CStatus(CIndex)
End Function

Private Sub Form_Unload(Cancel As Integer)

If Me.Job = "kill" Then GoTo UnloadCode

If FStatus.HTMLOpened = True Then
    MsgBox "Please close the Code Editor first.", vbInformation + vbMsgBoxSetForeground, "Closing..."
    Cancel = 1
    Exit Sub
End If

If FStatus.OpenFromROM = True Then
    If Trim(LCase(FStatus.HTMLString)) = Trim(LCase(DHTML1.DocumentHTML)) Then
        GoTo UnloadCode
    Else
        GoTo QuerySave
    End If
End If

On Error Resume Next
If Trim(LCase(FStatus.HTMLString)) = Trim(LCase(DHTML1.DocumentHTML)) Then
    'The document is clean
    GoTo UnloadCode
    
    Else
    'The document have been changed
QuerySave:

    Dim m As Integer
    m = MsgBox("Do you want to save changes on " & FStatus.FileTitle, vbQuestion + vbYesNoCancel, "Closing doucment...")
    
    Select Case m
        Case vbCancel
            Cancel = 1
            Exit Sub
            
        Case vbYes 'The most complicated part
            Dim s As Integer
            s = SaveFile(Me, MfrmProgram.cd1, FStatus, IIf(FStatus.OpenFromROM = False, Not FStatus.Saved, True))
            If s = 0 Then
                GoTo UnloadCode
            ElseIf s = cdlCancel Then
                Cancel = 1
                Exit Sub
            Else
                MsgBox Error(s), vbCritical, "File saving error: " & s
                Cancel = 1
                Exit Sub
            End If
            
        Case vbNo
            GoTo UnloadCode
            
    End Select
    
End If

UnloadCode:
    Me.Hide
    DoEvents
    Screen.MousePointer = 11
    On Error Resume Next
    Kill FStatus.TempFilename
    editor(CInt(Me.Tag)).Job = "kill"
    Unload editor(CInt(Me.Tag))
    Set editor(Me.Tag) = Nothing
    Screen.MousePointer = 0
    
    'If MfrmProgram.FileTab.Tabs.Count = 1 Then MfrmProgram.FileTab.Tabs.Clear
    MfrmProgram.FileTab.Tabs.Remove "key" & CInt(Me.Tag)

End Sub


Private Sub tbrQuick_ButtonClick(ByVal Button As MSComctlLib.Button)

If Button.Index <> 6 And Button.Index <> 7 Then
    ChangeMode Button.Index
    Exit Sub
End If

Select Case Button.Index
    Case 6
        DHTML1.ShowDetails = Not DHTML1.ShowDetails
        SaveOption "ShowDetails", DHTML1.ShowDetails
        Button.Value = IIf(DHTML1.ShowDetails = True, 1, 0)
    Case 7
        DHTML1.SnapToGrid = Not DHTML1.SnapToGrid
        SaveOption "SnapToGrid", DHTML1.SnapToGrid
        Button.Value = IIf(DHTML1.SnapToGrid = True, 1, 0)
End Select

End Sub

Public Function GetActiveElement() As Object
DoEvents
Dim rg As IHTMLTxtRange
Dim ctlRg As IHTMLControlRange
   
   On Error Resume Next
   Select Case DHTML1.DOM.selection.Type
      Case "None", "Text"
         ' This reduces the selection to just the insertion
         ' point. The parentElement method will then return the
         ' element directly under the mouse pointer.
         Set rg = DHTML1.DOM.selection.createRange
         rg.collapse False
         
         Set GetActiveElement = rg.parentElement
      Case "Control"
         ' A form or image is selected. The commonParentElement
         ' will return the site selected element.
         Set ctlRg = DHTML1.DOM.selection.createRange
         
         Set GetActiveElement = ctlRg.commonParentElement
         
         
   End Select
End Function

Private Function ElementP() As IHTMLElement
DoEvents
    On Error Resume Next
    'Set e = DHTML1.DOM.parentWindow.event
    Set ElementP = DHTML1.DOM.elementFromPoint(X, Y)

End Function

Public Function CreatorHTML() As String
'DHTML1.SaveDocument FStatus.TempFilename
CreatorHTML = DHTML1.DocumentHTML 'ReadText(FStatus.TempFilename)
End Function
